package com.suxii.netty_demo.activity;

import androidx.appcompat.app.AppCompatActivity;
import androidx.constraintlayout.widget.ConstraintLayout;

import android.os.Bundle;
import android.util.Log;
import android.view.KeyEvent;
import android.view.View;
import android.view.inputmethod.EditorInfo;
import android.view.inputmethod.InputMethodManager;
import android.widget.Button;
import android.widget.EditText;
import android.widget.TextView;
import android.widget.Toast;

import com.google.gson.Gson;
import com.google.gson.JsonObject;
import com.suxii.Thread.NettyThread;
import com.suxii.netty_demo.R;
import com.suxii.pojo.ChatMsg;
import com.suxii.pojo.FriendMsg;
import com.suxii.utils.Global;

import java.io.IOException;

import okhttp3.Call;
import okhttp3.OkHttpClient;
import okhttp3.Request;
import okhttp3.Response;

public class AddFriendActivity extends AppCompatActivity {

    private EditText text;
    private TextView btn;
    private ConstraintLayout res_layout;
    private TextView error_res;

    private TextView res_name;
    private TextView res_uid;

    @Override
    protected void onCreate(Bundle savedInstanceState) {
        super.onCreate(savedInstanceState);
        setContentView(R.layout.activity_add_friend);

        text = findViewById(R.id.addfriend_text);
        btn = findViewById(R.id.addfriend_btn);
        res_layout = findViewById(R.id.addfriend_res);
        error_res = findViewById(R.id.addfriend_error_res);
        res_name = findViewById(R.id.addfriend_res_name);
        res_uid = findViewById(R.id.addfriend_res_uid);

        addfriend_res_agree=findViewById(R.id.addfriend_res_agree);

        init();

    }

    private void init() {
        res_layout.setVisibility(View.GONE);
        btn.setOnClickListener(v -> {
            finish();
        });
        text.setOnKeyListener((v, keyCode, event) -> {
            //是否是回车键
            if (keyCode == KeyEvent.KEYCODE_ENTER && event.getAction() == KeyEvent.ACTION_DOWN) {
                //隐藏键盘
                ((InputMethodManager) getSystemService(INPUT_METHOD_SERVICE))
                        .hideSoftInputFromWindow(AddFriendActivity.this.getCurrentFocus()
                                .getWindowToken(), InputMethodManager.HIDE_NOT_ALWAYS);
                //搜索
                new Thread(() -> findFriend(text.getText().toString()), "查询用户").start();
            }
            return false;
        });
    }

    private void findFriend(String uid) {
        OkHttpClient client = new OkHttpClient();
        Request request = new Request.Builder()
                .url(getString(R.string.url) + "find-friend?uid=" + uid)
                .get()
                .build();
        Call call = client.newCall(request);
        try {
            Response execute = call.execute();
            Log.e("aa",execute.isSuccessful()+"http");
            String res = execute.body().string();
            JsonObject jsonObject = new Gson().fromJson(res, JsonObject.class);
            String data = String.valueOf(jsonObject.get("data"));
            if (!data.equals("null")) {
                Log.e("aa", data);
                JsonObject json = new Gson().fromJson(data, JsonObject.class);
                String name = String.valueOf(json.get("name"));
                String uuid = String.valueOf(json.get("uid"));
                updateView(true,name,uuid);
            } else {
                updateView(false,null,null);
            }

        } catch (IOException e) {
            e.printStackTrace();
        }

    }

    private void updateView(boolean isSuccess, String name, String uid) {
        AddFriendActivity.this.runOnUiThread(() -> {
            if (isSuccess) {
                res_name.setText(name);
                res_uid.setText(uid);
                sendFriendUid=Integer.parseInt(uid);
                res_layout.setVisibility(View.VISIBLE);
                error_res.setVisibility(View.GONE);
                addfriend_res_agree.setVisibility(View.VISIBLE);
            } else {
                error_res.setVisibility(View.VISIBLE);
                res_layout.setVisibility(View.GONE);
            }
        });

    }

    private TextView addfriend_res_agree;
    private Integer sendFriendUid;
    public void sendFriend(View view) {
        //发送好友申请
        NettyThread.channel.writeAndFlush(new FriendMsg(Global.uid,sendFriendUid));
        Toast.makeText(this, "已发送申请", Toast.LENGTH_SHORT).show();
        addfriend_res_agree.setVisibility(View.GONE);
    }

}